package com.tz.farmer.service.impl;


import com.alibaba.dubbo.common.utils.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.smartPark.msframe.result.ResultEx;
import com.tz.farmer.enums.ErrorCodeEnum;
import com.tz.farmer.mapper.FarmerCorrectMapper;
import com.tz.farmer.model.FarmerCorrect;
import com.tz.farmer.service.WebFarmerCorrectService;
import com.tz.farmer.so.FarmerCorrectSo;
import com.tz.farmer.util.BeanConvertUtils;
import com.tz.farmer.util.ResultExM;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;

@Service
public class WebFarmerCorrectServiceImpl extends ServiceImpl<FarmerCorrectMapper , FarmerCorrect> implements WebFarmerCorrectService {

     @Resource
    FarmerCorrectMapper farmerCorrectMapper;

    @Override
    public ResultEx getCorrectListByConditions(FarmerCorrectSo so) {
        if ( ObjectUtils.isEmpty(so) || so.getFlag()==null || so.getFlag() > 2 || "".equals(so.getFlag())) {
            return ResultExM.builder().makeResult(ErrorCodeEnum.ERR_COMMON_BAD_REQUEST);
        }
        int total = farmerCorrectMapper.countFlag(so.getFlag(),so.getClassify(), so.getKeyword(), so.getStartTime(), so.getEndTime());
        int n = (so.getCurrentPage()-1)*so.getPageSize();
        int m =so.getPageSize();
        List<FarmerCorrect> list = farmerCorrectMapper.selectConditionList(so.getFlag(),so.getClassify(), so.getKeyword(), so.getStartTime(), so.getEndTime(),n,m);
        Map<String, Object> map = new Hashtable<>();
        map.put("total", total);
        map.put("data", list);
        return ResultExM.builder().makeResult(ErrorCodeEnum.ERR_COMMON_SUCCEED, map);
    }

    @Transactional
    @Override
    public ResultEx updateAndAddCorrect(FarmerCorrectSo so) {
        if (ObjectUtils.isEmpty(so) || StringUtils.isEmpty(so.getDoFlag())) {
            return ResultExM.builder().makeResult(ErrorCodeEnum.ERR_COMMON_BAD_REQUEST);
        }
        if (so.getDoFlag().equals("1")) {
            FarmerCorrect farmerCorrect = BeanConvertUtils.copyProperties(so, FarmerCorrect.class);
            farmerCorrect.setProperty(so.getFlag());
            farmerCorrect.setReleaseTime(LocalDateTime.now());

            boolean save = this.save(farmerCorrect);
            if(save){
                return ResultExM.builder().makeResult(ErrorCodeEnum.ERR_COMMON_SUCCEED);
            }
            return ResultExM.builder().makeResult(ErrorCodeEnum.ERR_COMMON_FAIL);

        } else if (so.getDoFlag().equals("2")) {

            if (so.getId() == null || so.getId() <= 0) {
                return ResultExM.builder().makeResult(ErrorCodeEnum.ERR_COMMON_BAD_REQUEST);
            }
            FarmerCorrect farmerCorrect = BeanConvertUtils.copyProperties(so, FarmerCorrect.class);
            if(so.getIsSued() != null && so.getIsSued() == 0){
                farmerCorrect.setReleaseTime(LocalDateTime.now());
            }
            if(so.getIsTop() != null && so.getIsTop() == 1){
                int i = farmerCorrectMapper.selectMaxTop(so.getFlag());
                i=i+1;
                farmerCorrect.setTop(i);
            }
            boolean update = this.updateById(farmerCorrect);
            if (update) {
                return ResultExM.builder().makeResult(ErrorCodeEnum.ERR_COMMON_SUCCEED);
            }
            return ResultExM.builder().makeResult(ErrorCodeEnum.ERR_COMMON_UPDATE_FAIL_ERROR);
        }else if (so.getDoFlag().equals("3")) { //发布

            if (so.getId() == null || so.getId() <= 0) {
                return ResultExM.builder().makeResult(ErrorCodeEnum.ERR_COMMON_BAD_REQUEST);
            }
            FarmerCorrect farmerCorrect= farmerCorrectMapper.selectOne(new QueryWrapper<FarmerCorrect>().eq("id", so.getId()));
            if(ObjectUtils.isEmpty(farmerCorrect)
                    ||"".equals(farmerCorrect.getClassify())
                    || farmerCorrect.getClassify() == null
                    || "".equals(farmerCorrect.getTitle())
                    || farmerCorrect.getTitle() == null
                    || "".equals(farmerCorrect.getInformation())
                    || farmerCorrect.getInformation() == null
                    || "".equals(farmerCorrect.getInformation())
                    || farmerCorrect.getInformation() == null
                    || "".equals(farmerCorrect.getName())
                    || farmerCorrect.getName() == null){
                return ResultExM.builder().makeResult(ErrorCodeEnum.ERR_COMMON_ISSUE);
            }
            FarmerCorrect correct = BeanConvertUtils.copyProperties(so, FarmerCorrect.class);
            correct.setReleaseTime(LocalDateTime.now());
            boolean update = this.updateById(correct);
            if (update) {
                return ResultExM.builder().makeResult(ErrorCodeEnum.ERR_COMMON_SUCCEED);
            }
            return ResultExM.builder().makeResult(ErrorCodeEnum.ERR_COMMON_UPDATE_FAIL_ERROR);
        }else {
            return ResultExM.builder().makeResult(ErrorCodeEnum.ERR_COMMON_BAD_REQUEST);
        }

    }

    @Override
    public ResultEx getOneDetails(FarmerCorrectSo so) {
        if("".equals(so.getId()) || so.getId() == null){
            return ResultExM.builder().makeResult(ErrorCodeEnum.ERR_COMMON_BAD_REQUEST);
        }
        FarmerCorrect farmerCorrect = farmerCorrectMapper.selectById(so.getId());
        if(ObjectUtils.isEmpty(farmerCorrect)){
            return  ResultExM.builder().makeResult(ErrorCodeEnum.ERR_COMMON_NO_DATA);
        }
        return  ResultExM.builder().makeResult(ErrorCodeEnum.ERR_COMMON_SUCCEED,farmerCorrect);
    }

    @Override
    public ResultEx deleteOne(FarmerCorrectSo so) {
        if("".equals(so.getId()) || so.getId() == null){
            return ResultExM.builder().makeResult(ErrorCodeEnum.ERR_COMMON_BAD_REQUEST);
        }
        int id = farmerCorrectMapper.delete(new QueryWrapper<FarmerCorrect>().eq("id", so.getId()));
        if(id > 0){
            return ResultExM.builder().makeResult(ErrorCodeEnum.ERR_COMMON_SUCCEED);
        }

        return ResultExM.builder().makeResult(ErrorCodeEnum.ERR_COMMON_DELETE_FAIL_ERROR);
    }
}
